
#include <IRremote.h>

//#include <Tone.h>
#include "SoftTone.h"

const int ledStatusPin = 13;
const int tonePin      = 12;
const int irOutPin     = 3; // must be pin 3
const int irDetectPin  = 8; // can be any pin


IRsend irsend;
//Tone tone1;

void setup()
{
  pinMode( ledStatusPin, OUTPUT);
  pinMode( tonePin,      OUTPUT);
  pinMode( irOutPin,     OUTPUT);
  pinMode( irDetectPin,  INPUT);
  digitalWrite(irDetectPin, HIGH); // internal pullup

  irsend.enableIROut(38);
  irsend.mark(0);  // on

  /*
  tone1.begin(tonePin);

  tone1.play(NOTE_C3);
  delay(200);
  tone1.play(NOTE_C4);
  delay(200);
  tone1.play(NOTE_G3);
  delay(200);
  */

  playTone( tonePin, NOTE_C,  100 );
  playTone( tonePin, NOTE_C1, 100 );
  playTone( tonePin, NOTE_G,  100 );

  Serial.begin(19200);
  Serial.println("BeamBreak1");
}

void loop()
{
  loop3();
}

void loop3()
{

  //irsend.enableIROut(38);
  //irsend.mark(0);  // on

  int v = digitalRead(irDetectPin);
  digitalWrite(ledStatusPin, v);

  if( v == 1 ) { // detected
    long start = millis();
    while( v == 1 && ((millis()-start) < 1000) ) { 
      v = digitalRead(irDetectPin);
    }
    playTone( tonePin, NOTE_C, 50 );
    long dur = millis()- start;
    Serial.println( dur );
  }

  //irsend.space(0); // off

}

void loop2()
{
  //digitalWrite(PIN_STATUS, !digitalRead(PIN_DETECT));
  
  int v0 = digitalRead(irDetectPin);
  
  irsend.enableIROut(38);
  irsend.mark(0);  // turn on
  int v1 = digitalRead(irDetectPin);
  irsend.space(0); // turn off

  Serial.print( v0 ); Serial.print('-'); 
  Serial.print( v1 ); Serial.println();

  delay(10);
}

void loop1() 
{
  for( int i = 30; i<48 ; i++ ) { 
    irsend.enableIROut( i ) ;
    digitalWrite(ledStatusPin, !digitalRead(irDetectPin));
  }
}

void loop0()
{
  digitalWrite(ledStatusPin, !digitalRead(irDetectPin));
  delay(1);
}


